local luoshen = fk.CreateSkill {

  name = "joyex__luoshen",

  tags = { Skill.Compulsory, },

}



luoshen:addEffect(fk.EventPhaseStart, {
  name = "joyex__luoshen",
  anim_type = "drawcard",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(luoshen.name) and
      player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    while true do
      local judge = {
        who = player,
        reason = luoshen.name,
        pattern = ".|.|spade,club",
      }
      room:judge(judge)
      if judge.card.color ~= Card.Black then
        break
      end
      if not room:askForSkillInvoke(player, luoshen.name) then
        break
      end
    end
  end,
})

luoshen:addEffect(fk.AfterCardsMove, {
  name = "#joyex__luoshen_delay",
  mute = true,
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player.dead or not player:hasSkill(luoshen.name) or player:usedSkillTimes(luoshen.name, Player.HistoryRound) ~= 0 then return false end
    local mark = player:getMark("xingchong-round")
    for _, move in ipairs(data) do
      if move.from == player and move.extra_data and move.extra_data.joyex__luoshen then
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player,luoshen.name,nil,"是否立即发动一次〖洛神〗?")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local luoshens = Fk.skills["joyex__luoshen"]
    room:useSkill(player, luoshens, function()
        return luoshens:use(fk.EventPhaseStart, player, player)
      end)
  end,
})
luoshen:addEffect(fk.BeforeCardsMove, {
  refresh_events = {fk.BeforeCardsMove},
  can_refresh = function(self, event, target, player, data)
    if player.dead or not player:hasSkill(luoshen.name) or player:usedSkillTimes(luoshen.name, Player.HistoryRound) ~= 0 then return false end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and Fk:getCardById(info.cardId):getMark("@@joyex__luoshen-inhand") > 0 then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player then
        move.extra_data = move.extra_data or {}
        move.extra_data.joyex__luoshen = true
      end
    end
  end,
})

luoshen:addEffect(fk.FinishJudge, {
  name = "#joyex__luoshen_obtain",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.FinishJudge},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill("joyex__luoshen") and
      data.reason == "joyex__luoshen" 
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card.color == Card.Red then
      room:obtainCard(player, data.card,false,fk.ReasonJustMove,player,luoshen.name,"@@joyex__luoshen-inhand")
    else
      room:obtainCard(player, data.card)
    end
  end,
})

return luoshen